package aom.ju.ss.view.shua

import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentPagerAdapter
import androidx.viewpager.widget.ViewPager
import aom.ju.ss.R
import aom.ju.ss.databinding.ShopFragmentBinding
import aom.ju.ss.utils.VIV
import aom.ju.ss.utils.dp
import aom.ju.ss.utils.show
import aom.ju.ss.view.base.BaseActivity
import aom.ju.ss.view.base.BaseFragment
import aom.ju.ss.view.base.TopBar
import com.google.android.material.tabs.TabLayout


/**
 * 首页 刷店
 */
class HomeShuaFragment : BaseFragment() {
    val fragmentList: MutableList<Fragment> = arrayListOf()
    val title = arrayListOf("分类", "附近", "推荐")

    companion object {
        var my: HomeShuaFragment? = null


        fun getMys(): HomeShuaFragment {
            return HomeShuaFragment()
        }
    }

    lateinit var binding: ShopFragmentBinding

    val iconList = arrayListOf<View>()

    override fun onDestroy() {
        super.onDestroy()
        my = null
    }

    override fun init() {
        binding = viewBinding as ShopFragmentBinding

        my = this


        fragmentList.add(ClassFragment.newInstance())
        fragmentList.add(NearbyShopsFragment.newInstance())
        fragmentList.add(VideoFragment.getMys())

        binding.constraintLayout7.setPadding(0, TopBar.getStatusBarHeight(activity), 0, 0)


        binding.zhibo.setOnClickListener {

        }

        binding.btSs.setOnClickListener {
        }


        binding.virePager.adapter = object : FragmentPagerAdapter(childFragmentManager, 1) {
            override fun getItem(position: Int): Fragment {
                return fragmentList[position]
            }

            override fun getCount(): Int {
                return fragmentList.size
            }

        }
        binding.tabLayout.setupWithViewPager(binding.virePager)

        binding.virePager.offscreenPageLimit = 3
        initTab()

        val act = activity as BaseActivity
        binding.virePager.addOnPageChangeListener(object : ViewPager.OnPageChangeListener {
            override fun onPageScrolled(
                position: Int,
                positionOffset: Float,
                positionOffsetPixels: Int
            ) {
            }

            override fun onPageSelected(position: Int) {
                for ((i, icon) in iconList.withIndex()) {
                    icon.VIV(i == position)
                }
                val lm = binding.virePager.layoutParams as ConstraintLayout.LayoutParams

                binding.constraintLayout7.post {
                    binding.constraintLayout7.setBackgroundResource(if (position == 2) R.color.transparent else R.drawable.home_top_bg)
                    lm.setMargins(
                        0,
                        if (position == 2) 0 else binding.constraintLayout7.height,
                        0,
                        0
                    )
                    binding.virePager.layoutParams = lm
                }

                act.controller?.isAppearanceLightStatusBars = position == 2
            }

            override fun onPageScrollStateChanged(state: Int) {
            }
        })
        binding.virePager.offscreenPageLimit = 3
        binding.virePager.currentItem = 2

    }

    private fun initTab() {
        for (i in 0 until title.size) {
            val tab: TabLayout.Tab? = binding.tabLayout.getTabAt(i) //获得每一个tab
            tab?.let { tabs ->
                tabs.setCustomView(R.layout.tab_item) //给每一个tab设置view
                tabs.customView?.let {
                    val tv = it.findViewById(R.id.tabtext) as TextView
                    val icon = it.findViewById(R.id.tabicon) as ImageView
                    tv.text = title[i] //第一个tab被选中
                    icon.VIV(i == 2)
                    iconList.add(icon)
                }
            }
        }
    }


    override fun getLay(): Int {
        return R.layout.shop_fragment
    }
}


